package DAO;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import Hibernate.HibernateUtil;
import Negocio.PrecioCarrier;

public class HibernatePrecioCarrierDAO 
{
	private static HibernatePrecioCarrierDAO instancia = null;
	private static SessionFactory sf = null;
	
	
	public static HibernatePrecioCarrierDAO getInstancia()
	{
		if(instancia == null)
		{
			instancia = new HibernatePrecioCarrierDAO();
			sf = HibernateUtil.getSessionFactory();
		}
		return instancia;
	}
	
	public PrecioCarrier getPrecioCarrier(int nro)
	{
        try 
        {
        	Session session=sf.openSession();
            session.beginTransaction();
            PrecioCarrier factura = (PrecioCarrier)session.get(PrecioCarrier.class, nro);
            session.getTransaction().commit();
            session.close();
            return factura;
        } catch (Exception e){
            System.out.println(e);
            System.out.println("Error al cargar la Factura");
        }
        return null;
	}

	public void grabarPrecioCarrier(PrecioCarrier pCarrier) {
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(pCarrier);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch(Exception e){
			System.out.println(e);
			System.out.println("Error al grabar una Factura");
		}
	}
	
	public void updatePrecioCarrier(PrecioCarrier pCarrier){
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(pCarrier);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch (Exception e){
			System.out.println(e);
            System.out.println("Error al actualizar una Factura");
		}
	}
}
